/**
 * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
 *   
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.afterturn.easypoi.word;

import cn.afterturn.easypoi.word.entity.bookmark.InitWordEntity;
import cn.afterturn.easypoi.word.entity.bookmark.WordEntityVO;
import cn.afterturn.easypoi.word.parse.Algorithm.FactoryParseThisParagraph;
import cn.afterturn.easypoi.word.parse.Algorithm.ParseParagraphAlgo;
import cn.afterturn.easypoi.word.parse.OfficeConstants;
import cn.afterturn.easypoi.word.parse.ParseWord07BookMark;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Word使用模板导出工具类
 * 
 * @author JueYue
 *  2013-11-16
 * @version 1.0
 */
public class WordExportUtilExtH {

    private WordExportUtilExtH() {

    }
    /**
     * 解析Word2007版本
     *Steven版本，2020-02-18
     * @param url
     *            模板地址
     * 获取Wor中的所有的bookmark
     * @return
     */
    public static WordEntityVO getWord07BookMark(InputStream inputStream, String url) throws Exception {
        Map<String, Object> map =new HashMap<>();
        String color="ffff00";
        WordEntityVO wordEntityVO =InitWordEntity.initWordEntity(url);
        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();
        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.GET_WORDALLBOOKMARK);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(inputStream, map, color);
        wordEntityVO =parseWord07BookMark.getWordEntityVO();
        return wordEntityVO;
    }
    /**
     * 解析Word2007版本
     *Steven版本，2020-02-18
     * @param url
     *            模板地址
     * 获取Wor中的所有的bookmark
     * @return
     */
    public static WordEntityVO getWord07BookMark(String url) throws Exception {
        Map<String, Object> map =new HashMap<>();
        String color="ffff00";
        WordEntityVO wordEntityVO =InitWordEntity.initWordEntity(url);
        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();

        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.GET_WORDALLBOOKMARK);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        boolean userInputStream=true;
        if(userInputStream){
            InputStream initialStream = new FileInputStream(new File(url));
            parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(initialStream, map, color);
        }else {
            XWPFDocument document = parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(url, map, color);
        }
        wordEntityVO =parseWord07BookMark.getWordEntityVO();
        return wordEntityVO;

    }
    /**
     * 解析Word2007版本
     *Steven版本，2020-02-18
     * @param url
     *            模板地址
     *            Word中高亮显示标签

     * @return
     */
    public static XWPFDocument HighLightexportWord07BookMark(String url,String color) throws Exception {


        WordEntityVO wordEntityVO =InitWordEntity.initWordEntity(url);
        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();
        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.SET_WORDALLBOOKMARKCOLOR);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        Map<String,Object> map=new HashMap<>();
        return parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(url, map,color);
        //return parseWord07BookMark.getWordEntityVO();
    }
    /**
     * 解析Word2007版本
     *Steven版本，2020-02-08
     * 将标签的值写入到Word中
     * @param url
     *            模板地址
     * @param map
     *            解析数据源
     * @return
     */
    public static XWPFDocument exportWord07BookMarkExt(String url, Map map, String color) throws Exception {

        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();
        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.SET_WORDALLBOOKMARKVALUECOLOR);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        return parseWord07BookMark.parseWord(url, map,color);
        //return parseWord07BookMark.getWordEntityVO();
    }

    /**
     * 解析Word2007版本
     *Steven版本，2020-02-08
     * 将标签的值写入到Word中
     * @param url
     *            模板地址
     * @param map
     *            解析数据源
     * @return
     */
    public static XWPFDocument exportWord07BookMark(String url, Map map, WordEntityVO wordEntityVO, String color) throws Exception {
        wordEntityVO =InitWordEntity.initWordEntity(url);
        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();
        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.SET_WORDALLBOOKMARKVALUECOLOR);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        return parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(url, map,color);
        //return parseWord07BookMark.getWordEntityVO();
    }

    /**
     * 一个模板生成多页
     * @param url
     * @param list
     * @return
     * @throws Exception
     */
    public static XWPFDocument exportWord07(String url, List<Map<String, Object>> list,String color) throws Exception {
        WordEntityVO wordEntityVO =InitWordEntity.initWordEntity(url);
        ParseWord07BookMark parseWord07BookMark=new ParseWord07BookMark();
        ParseParagraphAlgo parseParagraphAlgo= FactoryParseThisParagraph.getparseParagraphAlgo(OfficeConstants.SET_WORDALLBOOKMARKVALUECOLOR);
        parseWord07BookMark.setParagraphAlgo(parseParagraphAlgo);
        return parseWord07BookMark.setWordEntityVO(wordEntityVO).parseWord(url,list,color);

    }
}
